package com.ruoyi.safe.controller.web;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.safe.domain.BizProjectCheck;
import com.ruoyi.safe.domain.BizProjectCheckRecord;
import com.ruoyi.safe.domain.BizRectifyReview;
import com.ruoyi.safe.domain.dto.BizProjectCheckRecordSearchDTO;
import com.ruoyi.safe.domain.dto.CheckRecordWebDTO;
import com.ruoyi.safe.domain.vo.DataVO;
import com.ruoyi.safe.domain.vo.StatisticsAndDataVO;
import com.ruoyi.safe.service.IBizProjectCheckRecordService;
import com.ruoyi.safe.service.IBizProjectCheckService;
import com.ruoyi.safe.service.IBizRectifyReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 项目自检-检查记录Controller
 *
 * @author ruoyi
 * @date 2023-11-20
 */
@RestController
@RequestMapping("/record/record")
public class BizProjectCheckRecordController extends BaseController {
    @Autowired
    private IBizProjectCheckRecordService bizProjectCheckRecordService;
    @Autowired
    private IBizRectifyReviewService bizRectifyReviewService;
    @Autowired
    private IBizProjectCheckService bizProjectCheckService;

    /**
     * 查询项目自检-检查记录列表
     */
    @PreAuthorize("@ss.hasPermi('record:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam("id") Long projectCheckId) {
        startPage();
//        List<BizProjectCheckRecord> list = bizProjectCheckRecordService.selectBizProjectCheckRecordList(bizProjectCheckRecord);
        List<BizProjectCheckRecord> list = bizProjectCheckRecordService.selectListByProjectCheckId(projectCheckId);
        return getDataTable(list);
    }

    /**
     * 导出项目自检-检查记录列表
     */
    @PreAuthorize("@ss.hasPermi('record:record:export')")
    @Log(title = "项目自检-检查记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizProjectCheckRecord bizProjectCheckRecord) {
        List<BizProjectCheckRecord> list = bizProjectCheckRecordService.selectBizProjectCheckRecordList(bizProjectCheckRecord);
        ExcelUtil<BizProjectCheckRecord> util = new ExcelUtil<>(BizProjectCheckRecord.class);
        util.exportExcel(response, list, "项目自检-检查记录数据");
    }

    /**
     * 获取项目自检-检查记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('record:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        //查询到隐患记录+复查/整改数据
        BizProjectCheckRecord bizProjectCheckRecord = bizProjectCheckRecordService.selectBizProjectCheckRecordById(id);
        BizProjectCheck bizProjectCheck = bizProjectCheckService.selectListByCheckId(bizProjectCheckRecord.getProjectCheckId());
        //复查数据 和 整改数据
        List<BizRectifyReview> bizRectifyReviews = bizRectifyReviewService.selectOperateList(id);
        DataVO dataVO = new DataVO();
        dataVO.setBizProjectCheckRecord(bizProjectCheckRecord);
        dataVO.setBizRectifyReviews(bizRectifyReviews);
        dataVO.setBizProjectCheck(bizProjectCheck);
        return success(dataVO);
    }

    /**
     * 新增项目自检-检查记录
     */
    @PreAuthorize("@ss.hasPermi('record:record:add')")
    @Log(title = "项目自检-检查记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizProjectCheckRecord bizProjectCheckRecord) {
        return toAjax(bizProjectCheckRecordService.insertBizProjectCheckRecord(bizProjectCheckRecord));
    }

    /**
     * 修改项目自检-检查记录
     */
    @PreAuthorize("@ss.hasPermi('record:record:edit')")
    @Log(title = "项目自检-检查记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizProjectCheckRecord bizProjectCheckRecord) {
        return toAjax(bizProjectCheckRecordService.updateBizProjectCheckRecord(bizProjectCheckRecord));
    }

    /**
     * 删除项目自检-检查记录
     */
    @PreAuthorize("@ss.hasPermi('record:record:remove')")
    @Log(title = "项目自检-检查记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bizProjectCheckRecordService.deleteBizProjectCheckRecordByIds(ids));
    }

    /**
     * 获取隐患任务的排查信息
     */
    @GetMapping("/getCheckInfo")
    public AjaxResult getCheckInfo(Long projectId, Long planId, String hiddenDangerStatus) {
        return success(bizProjectCheckRecordService.selectBizProjectCheckRecord(projectId, planId, hiddenDangerStatus));
    }

    /**
     * 安全检查台账
     */
    @GetMapping("/getCheckLedger")
    public AjaxResult getCheckLedger(BizProjectCheckRecordSearchDTO searchDTO) {
        startPage();
        StatisticsAndDataVO statisticsAndDataVO = bizProjectCheckRecordService.selectCheckLedger(searchDTO);
        TableDataInfo dataTable = getDataTable(CollectionUtil.isEmpty(statisticsAndDataVO.getList()) ? CollectionUtil.newArrayList() : statisticsAndDataVO.getList());
        statisticsAndDataVO.setDataTable(dataTable);
        statisticsAndDataVO.setList(null);

        return success(statisticsAndDataVO);
    }

    /**
     * 获取安全隐患详情
     */
    @GetMapping("/getCheckLedgerDetail")
    public AjaxResult getCheckLedgerDetail(Long id, Long projectCheckId) {
        BizProjectCheckRecord bizProjectCheckRecord = bizProjectCheckRecordService.selectBizProjectCheckRecordVO2ById(id, projectCheckId);
        return success(bizProjectCheckRecord);
    }

    /**
     * 检查人列表
     */
    @GetMapping("/checkPeopleList")
    public AjaxResult getCheckPeopleList() {
        return AjaxResult.success(bizProjectCheckRecordService.selectCheckPeopleList());
    }

    /**
     * 补录整改数据or排查数据
     */
    @PostMapping("/addCheckRecord")
    public AjaxResult addCheckRecord(@RequestBody CheckRecordWebDTO checkRecordWebDTO) {
        if (ObjectUtil.isNull(checkRecordWebDTO.getProjectId())) {
            throw new RuntimeException("项目id不能为空");
        }
        return toAjax(bizProjectCheckRecordService.addCheckRecord(checkRecordWebDTO));
    }


}
